import 'package:flutter/material.dart';
import 'package:flutter_yn_goods/config/app_color.dart';
import 'package:flutter_yn_goods/config/app_config.dart';
import 'package:flutter_yn_goods/config/app_routes.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/generated/assets.dart';
import 'package:flutter_yn_goods/user/app_user_manager.dart';
import 'package:flutter_yn_goods/widgets/appbar/common_app_bar.dart';
import 'package:flutter_yn_goods/widgets/common_normal_dialog.dart';
import 'package:flutter_yn_goods/widgets/image/auto_image.dart';
import 'package:flutter_yn_goods/widgets/image/image_view.dart';
import 'package:flutter_yn_goods/widgets/widget_language_dialog.dart';
import 'package:flutter_yn_goods/widgets/widget_line.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'mine_logic.dart';

class MinePage extends StatelessWidget {
  final logic = Get.put(MineLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        extendBodyBehindAppBar: true,
        appBar: CommonAppBar(
          title: LocaleKeys.mine.tr,
          showLeft: false,
          actions: [
            IconButton(
              icon: Icon(
                Icons.settings,
                color: AppColor.color181818,
                size: 24.w,
              ),
              onPressed: () {
                Get.toNamed(AppRoutes.settingsPage);
              },
            ),
          ],
        ),
        body: Obx(() {
          return Stack(children: [
            Positioned(
              top: 0,
              left: 0,
              right: 0,
              child: Container(
                  height: ScreenUtil().statusBarHeight + 44 + 160.w,
                  decoration: const BoxDecoration(
                      // color: Colors.red,
                      image: DecorationImage(
                          image: AssetImage(
                            Assets.mineTopBg,
                          ),
                          fit: BoxFit.cover)),
                  child: Column(
                    children: [
                      GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () {
                          if (!AppUserManager().isLogin) {
                            Get.toNamed(AppRoutes.login);
                          }
                        },
                        child: Container(
                            height: 64.w,
                            margin: EdgeInsets.only(
                                top: ScreenUtil().statusBarHeight + 44 + 18.w,
                                left: 12.w,
                                right: 12.w),
                            child: Row(
                              // crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                (AppUserManager().isLogin &&
                                        (AppUserManager().user.value?.avatar ??
                                                '')
                                            .isNotEmpty)
                                    ? ImageView(
                                        imageUrl:
                                            AppUserManager().user.value?.avatar,
                                        radius: 8,
                                        width: 64.w,
                                        height: 64.w,
                                      )
                                    : AutoImage(
                                        Assets.mineHeader,
                                        // imageUrl: AppUserManager().user.value?.avatar,
                                        width: 64.w,
                                        height: 64.w,
                                        // radius: 8,
                                      ),

                                10.horizontalSpace,
                                Expanded(
                                    child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Text(
                                      AppUserManager().isLogin
                                          ? (AppUserManager()
                                                  .user
                                                  .value
                                                  ?.nickname ??
                                              '')
                                          : LocaleKeys.not_logged_in.tr,
                                      style: TextStyle(
                                        color: AppColor.color282828,
                                        fontSize: 16.sp,
                                        fontWeight: FontWeight.w500,
                                      ),
                                    ),
                                    if (!AppUserManager().isLogin)
                                      Text(
                                        LocaleKeys.click_to_login.tr,
                                        style: TextStyle(
                                          color: AppColor.color616166,
                                          fontSize: 12.sp,
                                          fontWeight: FontWeight.w400,
                                        ),
                                      ),
                                  ],
                                )),
                                10.horizontalSpace,
                                // Icon(
                                //   Icons.arrow_forward_ios,
                                //   weight: 12.w,
                                //   color: AppColor.color999999,
                                // )
                              ],
                            )),
                      )
                    ],
                  )),
            ),
            Positioned(
                bottom: 0,
                right: 0,
                left: 0,
                top: ScreenUtil().statusBarHeight + 44 + 92.w,
                child: Container(
                  padding:
                      EdgeInsets.symmetric(horizontal: 27.5.w, vertical: 12.w),
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                        topRight: Radius.circular(18.w),
                        topLeft: Radius.circular(18.w),
                      )),
                  child: Column(
                    children: [
                      _buildItem(Assets.mineRowUser, LocaleKeys.profile.tr,
                          onClick: () {
                        if (AppUserManager().isLogin) {
                          Get.toNamed(AppRoutes.mineEditUserPage);
                        } else {
                          Get.toNamed(AppRoutes.login);
                        }
                      }),
                      _buildItem(
                          Icons.favorite_border, LocaleKeys.my_collections.tr,
                          isIcon: true, onClick: () {
                        if (AppUserManager().isLogin) {
                          Get.toNamed(AppRoutes.mineCollectPage);
                        } else {
                          Get.toNamed(AppRoutes.login);
                        }
                      }),

                      _buildItem(Icons.history, LocaleKeys.my_visit_records.tr,
                          isIcon: true, onClick: () {
                        if (AppUserManager().isLogin) {
                          Get.toNamed(AppRoutes.mineVisitPage);
                        } else {
                          Get.toNamed(AppRoutes.login);
                        }
                      }),

                      if (AppUserManager().isLeader())
                        _buildItem(
                            Icons.list_alt, LocaleKeys.agent_order_list.tr,
                            isIcon: true, onClick: () {
                          if (AppUserManager().isLogin) {
                            Get.toNamed(AppRoutes.agentOrderListPage);
                          } else {
                            Get.toNamed(AppRoutes.login);
                          }
                        }),
                      if (AppUserManager().isLeader())
                        _buildItem(Icons.check_circle_outline,
                            LocaleKeys.completed_orders.tr, isIcon: true,
                            onClick: () {
                          if (AppUserManager().isLogin) {
                            Get.toNamed(AppRoutes.completedOrdersPage);
                          } else {
                            Get.toNamed(AppRoutes.login);
                          }
                        }),

                      // _buildItem(
                      //     Assets.mineRowService, LocaleKeys.contact_support.tr,
                      //     onClick: () {
                      //   logic.openEmailApp();
                      // }),
                      // _buildItem(Assets.mineRowAbout, LocaleKeys.about_us.tr,
                      //     onClick: () {
                      //   Get.toNamed(AppRoutes.aboutUsPage);
                      // }),
                      // _buildItem(
                      //     Assets.mineRowLangue, LocaleKeys.change_language.tr,
                      //     onClick: () {
                      //   const WidgetLanguageDialog().show();
                      // }),
                      // _buildItem(
                      //     Assets.mineRowCancel, LocaleKeys.cancel_account.tr,
                      //     textColor: Colors.red, onClick: () {
                      //   if (AppUserManager().isLogin) {
                      //     logic.cancelAccount();
                      //   } else {
                      //     Get.toNamed(AppRoutes.login);
                      //   }
                      // }),
                      // _buildItem(Assets.exit, LocaleKeys.logout.tr,
                      //     onClick: () {
                      //   if (AppUserManager().isLogin) {
                      //     logic.loginOut();
                      //   } else {
                      //     Get.toNamed(AppRoutes.login);
                      //   }
                      // }),
                      // _buildItem(
                      //     Assets.mineRowVersion,
                      //     info: AppConfig.version,
                      //     LocaleKeys.current_version.tr,
                      //     onClick: () {}),
                    ],
                  ),
                ))
          ]);
        }));
  }

  Widget _buildItem(dynamic icon, String title,
      {required Function onClick,
      String? info,
      Color? textColor,
      bool isIcon = false}) {
    return GestureDetector(
      onTap: () {
        onClick.call();
      },
      behavior: HitTestBehavior.opaque,
      child: SizedBox(
        height: 54.w,
        child: Column(
          children: [
            Expanded(
                child: Row(
              children: [
                isIcon
                    ? Icon(
                        icon,
                        size: 22.w,
                        color: AppColor.color191D20,
                      )
                    : AutoImage(
                        icon,
                        width: 22.w,
                      ),
                10.horizontalSpace,
                Expanded(
                    child: Text(
                  title,
                  style: TextStyle(
                      color: textColor ?? AppColor.color191D20,
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w500),
                )),
                10.horizontalSpace,
                info != null
                    ? Text(
                        info,
                        style: TextStyle(
                            color: AppColor.color191D20,
                            fontSize: 14.sp,
                            fontWeight: FontWeight.w500),
                      )
                    : Icon(
                        Icons.arrow_forward_ios,
                        weight: 10.w,
                        color: AppColor.color191D20,
                      )
              ],
            )),
            const WidgetLine(),
          ],
        ),
      ),
    );
  }
}
